package com.tansun.easycare.rule.utils;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageInfo;
import com.tansun.easycare.rule.rulegroup.model.RuleRefReq;

public class PageUtils {

	public static <T> PageInfo<T> createSimglePageInfo(List<T> list){
		if(list == null) {
			list = new ArrayList<>();
		}
		PageInfo<T> pageInfo = new PageInfo();
		pageInfo.setList(list);
	    pageInfo.setSize(list.size());
	    pageInfo.setEndRow(list.size());
	    pageInfo.setTotal(list.size());
	    pageInfo.setPageNum(1);
	    pageInfo.setPageSize(10);
	    pageInfo.setStartRow(1);
	    pageInfo.setPages(1);
	    pageInfo.setPrePage(0);
	    pageInfo.setNextPage(0);
	    pageInfo.setIsFirstPage(true);
	    pageInfo.setIsLastPage(true);
	    pageInfo.setHasPreviousPage(false);
	    pageInfo.setHasNextPage(false);
	    pageInfo.setNavigatePages(8);
	    pageInfo.setNavigateFirstPage(1);
	    pageInfo.setNavigateLastPage(1);
	    pageInfo.setNavigatepageNums(new int[]{1});
	    return pageInfo;
	}
	
	public static <T> PageInfo<T> createSimglePageInfo(List<T> list,RuleRefReq ruleRefReq){
		if(list == null) {
			list = new ArrayList<>();
		}
		int pageNum = ruleRefReq.getPageNum();
		int pageSize = ruleRefReq.getPageSize();
		int startRow = 0;
		int endRow = 0;
		int pages = 0;
		int total = list.size();
		boolean isFirstPage = false;
		boolean isLastPage = false;
		boolean hasPreviousPage = true;
		boolean hasNextPage = true;
		int prePage = 0;
		int nextPage = 0;
		//全部之和为0是，说明是第一次查询，此时应该返回默认值，
		//即每页数据量pageSize =10，当前页pageNum=1，数据从startRow=0开始
		if( (pageNum+pageSize+startRow+endRow+pages) == 0) {
			pageSize = 10;
			pageNum = 1;
			startRow = 0;
			if(total > pageSize) {
				endRow = pageSize;
				pages = total/pageSize +1;
				hasPreviousPage = true;
				isLastPage = false;
				hasNextPage= true;
				prePage=pageNum-1;
				nextPage=pageNum+1;
			}else {
				endRow = total;
				pages = 1;
				isLastPage = true;
				hasPreviousPage = false;
				hasNextPage = false;
				prePage=pageNum-1;
				nextPage=0;
			}
			isFirstPage = true;
		}else {
			startRow = (pageNum-1)*pageSize;
			endRow = pageNum*pageSize;
			pages = total/pageSize + 1;
			if(total > endRow) {
				hasPreviousPage = true;
				isLastPage = false;
				hasNextPage= true;
				prePage=pageNum-1;
				nextPage=pageNum+1;
			}else {
				endRow = total;
				isLastPage = true;
				hasPreviousPage = startRow > 0;
				hasNextPage = false;
				prePage=pageNum-1;
				nextPage=0;
			}
		}
		List<T> newList = list.subList(startRow, endRow);
		PageInfo<T> pageInfo = new PageInfo();
		pageInfo.setList(newList);
	    pageInfo.setSize(total);
	    pageInfo.setEndRow(endRow);
	    pageInfo.setTotal(total);
	    pageInfo.setPageNum(pageNum);
	    pageInfo.setPageSize(pageSize);
	    pageInfo.setStartRow(startRow);
	    pageInfo.setPages(pages);
	    pageInfo.setPrePage(prePage);
	    pageInfo.setNextPage(nextPage);
	    pageInfo.setIsFirstPage(isFirstPage);
	    pageInfo.setIsLastPage(isLastPage);
	    pageInfo.setHasPreviousPage(hasPreviousPage);
	    pageInfo.setHasNextPage(hasNextPage);
	    pageInfo.setNavigatePages(8);
	    pageInfo.setNavigateFirstPage(1);
	    pageInfo.setNavigateLastPage(1);
	    pageInfo.setNavigatepageNums(new int[]{1});

	    return pageInfo;
	}
}
